<?php   
class ControllerCommonHeader extends Controller {	
	protected function index() {
		$this->data['title'] = $this->document->getTitle();
		
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$this->data['base'] = $this->config->get('config_ssl');
		} else {
			$this->data['base'] = $this->config->get('config_url');
		}		
		$this->data['description'] = $this->document->getDescription();
		$this->data['keywords'] = $this->document->getKeywords();
		$this->data['links'] = $this->document->getLinks();	 
		$this->data['styles'] = $this->document->getStyles();
		$this->data['scripts'] = $this->document->getScripts();
		$this->data['lang'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
		
		
		$this->data['abouturl'] = $this->config->get('abouturl');
		$this->data['deliveryurl'] = $this->config->get('deliveryurl');    
		$this->data['privacyurl'] = $this->config->get('privacyurl');    
		$this->data['termsurl'] = $this->config->get('termsurl');  
		$this->data['about_title'] = $this->language->get('about_title');  
		$this->data['delivery_title'] = $this->language->get('delivery_title');  
		$this->data['privacy_title'] = $this->language->get('privacy_title');  
		$this->data['terms_title'] = $this->language->get('terms_title');         
		
		$this->language->load('common/header');
		$this->data['button_login'] = $this->language->get('button_login');    
		$this->data['text_register'] = $this->language->get('text_register');    
		
		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = HTTP_IMAGE . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}
		
		$this->data['name'] = $this->config->get('config_name');
				
		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = HTTP_IMAGE . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}
		
		
		
		$this->data['text_home'] = $this->language->get('text_home');
		
		$this->data['text_language'] = $this->language->get('text_language');  		
		
		$this->data['languagesupport'] = $this->config->get('languagesupport');  
				
		$this->data['home'] = $this->url->link('common/home');
		
		$this->data['text_information'] = $this->language->get('text_information');
		
		$this->data['action'] = $this->url->link('common/home');

		if (!isset($this->request->get['route'])) {
			$this->data['redirect'] = $this->url->link('common/home');
		} else {
			$data = $this->request->get;
			
			unset($data['_route_']);
			
			$route = $data['route'];
			
			unset($data['route']);
			
			$url = '';
			
			if ($data) {
				$url = '&' . urldecode(http_build_query($data));
			}			
			
			$this->data['redirect'] = $this->url->link($route, $url);
		}

    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['language_code'])) {
			$this->session->data['language'] = $this->request->post['language_code'];
		
			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect($this->url->link('common/home'));
			}
    	}		
						
		$this->data['language_code'] = $this->session->data['language'];
		
		$this->load->model('localisation/language');
		
		$this->data['languages'] = array();
		
		$results = $this->model_localisation_language->getLanguages();
		
		foreach ($results as $result) {
			if ($result['status']) {
				$this->data['languages'][] = array(
					'name'  => $result['name'],
					'code'  => $result['code'],
					'image' => $result['image']
				);	
			}
		}

		
		// Menu
		$this->load->model('catalog/menu');
		;
		
		$this->data['menu'] = array();
					
		$menu = $this->model_catalog_menu->getAllMenu();
		
		foreach ($menu as $item) {			
				$children_data = array();				
				$children = $this->model_catalog_menu->getAllMenu($item['menu_id']);				
				foreach ($children as $child) {
					$data = array(
						'filter_menu_id'  => $child['menu_id'],
						'filter_sub_menu' => true	
					);			
									
					$children_data[] = array(
						'name'  => $child['name'] ,
						'display'	=>	$child['new'],
						'href'  => $child['url']
					);					
				}
				
				// Level 1
				$this->data['menu'][] = array(
					'name'     => $item['name'],
					'children' => $children_data,
					'display'	=>	$item['new'],
					'href'     => $item['url']
				);
			}
		

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/header.tpl';
		} else {
			$this->template = 'default/template/common/header.tpl';
		}
		
    	$this->render();
	} 	
}
?>